﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KorkolaskuriBL
{
    public class EqualPrincipalPortion : IPaymentStrategy
    {
        public EqualPrincipalPortion()
        {
        }

        public string Name
        {
            get { return Resources.Strings.EqualPrincipalPortionName; }
        }

        public IEnumerable<LoanPayment> CalculatePayments(
            double initialBalance, double interestRate, int interestPeriods, int paymentPeriods, double periodicPayment)
        {
            double balance = initialBalance;
            double periodicIntrest = interestRate / interestPeriods;
            double principalPortion;
            double interestPortion;

            for (int i = 0; i < paymentPeriods; i++) {
                principalPortion = periodicPayment;
                interestPortion = balance * interestRate / interestPeriods;
                if (principalPortion < balance)
                    balance -= principalPortion;
                else
                    balance -= balance;
                yield return new LoanPayment(
                    i + 1,
                    periodicPayment,
                    interestPortion,
                    balance);
            }
        }

        public double CalculatePeriodicPayment(
            double initialBalance, double interestRate, int interestPeriods, int paymentPeriods)
        {
            return initialBalance / paymentPeriods;
        }

        public int CalculatePaymentPeriods(
            double initialBalance, double interestRate, int interestPeriods, double periodicPayment)
        {
            return (int)Math.Ceiling(initialBalance / periodicPayment);
        }
    }
}
